package com.tutu.flow.service.impl;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.tutu.common.GlobalConstants;
import com.tutu.flow.dao.BpmFormMapper;
import com.tutu.flow.dao.BpmStockHolderMapper;
import com.tutu.flow.entity.BpmForm;
import com.tutu.flow.entity.BpmFormExample;
import com.tutu.flow.entity.BpmFormExample.Criteria;
import com.tutu.flow.entity.BpmStockHolder;
import com.tutu.flow.service.IBpmFormService;

@Service
public class BpmFormServiceImpl implements IBpmFormService {
	
	protected Logger logger = LoggerFactory.getLogger(getClass());
	@Resource
	private RepositoryService repositoryService;
	@Resource
	private RuntimeService runtimeService;
	
	@Resource
	private BpmFormMapper bpmFormMapper;
	@Resource
	private BpmStockHolderMapper bpmStockHolderMapper;

	@Override
	public void saveProcessInstance(String processInstanceKey, BpmForm bpmForm)
			throws Exception {
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put(GlobalConstants.FLOW_FROM_VAR, bpmForm);
		
		ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey(BpmForm.OTHER_BUSINESS).latestVersion().singleResult();
		ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId(), variables);
		
		bpmForm.setProcInstId(processInstance.getId());
		this.bpmFormMapper.insert(bpmForm);
		for(BpmStockHolder bpmStockHolder : bpmForm.getBpmStockHolders()){
			if(StringUtils.isBlank(bpmStockHolder.getName())){
				continue;
			}
			
			bpmStockHolder.setFormId(bpmForm.getFormId());
			bpmStockHolder.setAliveFlag(bpmForm.getAliveFlag());
			bpmStockHolder.setCreator(bpmForm.getCreator());
			bpmStockHolder.setCreateTime(bpmForm.getCreateTime());
			this.bpmStockHolderMapper.insert(bpmStockHolder);
		}
	}

	@Override
	public String getNewBusinessNo() throws Exception {
		String currentDateStr = DateFormatUtils.format(Calendar.getInstance(), "yyyyMMdd");
		BpmFormExample bpmFormExample = new BpmFormExample();
		Criteria criteria = bpmFormExample.createCriteria();
		criteria.andBusinessNoLike(currentDateStr+"%");
		bpmFormExample.setOrderByClause("BUSINESS_NO DESC");
		List<BpmForm> bpmForms = this.bpmFormMapper.selectByExample(bpmFormExample);
		String newBusinessNo = null;
		String abbr = "001";
		if(CollectionUtils.isNotEmpty(bpmForms)){
			BpmForm bpmForm = bpmForms.get(0);
			abbr = StringUtils.substring(bpmForm.getBusinessNo(), 8);
			if(NumberUtils.isDigits(abbr)){
				abbr = StringUtils.leftPad(Integer.toString(NumberUtils.toInt(abbr)+1), 3, "0"); 
			}else{
				abbr = "001";
			}
		}
		newBusinessNo = currentDateStr + abbr;
		return newBusinessNo;
	}

}
